#!/usr/bin/perl

use Test;
BEGIN { plan tests => 2 }

$basedir = $0;
$basedir =~ s|(.*)/[^/]*|$1|;

# Start the process that will have its priority and scheduling changed.
system("mkfifo $basedir/flag");
if ( ( $pid = fork() ) == 0 ) {
    exec
"runcon -t test_setsched_target_t sh -c 'echo >$basedir/flag; while :; do sleep 1; done'";
}

# Wait for it to start.
system("read -t 5 <>$basedir/flag");

$cgroup_cpu = "/sys/fs/cgroup/cpu/tasks";
if ( -w $cgroup_cpu ) {

    # We can only set the scheduler policy fo SCHED_{RR,FIFO} in the root
    # cgroup so move our target process to the root cgroup.
    open( my $fd, ">>", $cgroup_cpu );
    print $fd $pid;
    close $fd;
}

# Verify that test_setsched_yes_t can change the scheduling.
# SCHED_OTHER	0	priority must == 0
# SCHED_FIFO	1	priority 1..99
# SCHED_RR	2	priority 1..99
$result =
  system "runcon -t test_setsched_yes_t -- $basedir/source $pid 2 1 2>&1";
ok( $result, 0 );

# Verify that test_setsched_no_t cannot change the scheduling.
$result =
  system "runcon -t test_setsched_no_t -- $basedir/source $pid 2 1 2>&1";
ok($result);

# Kill the target
kill KILL, $pid;

# Clean up.
system "rm -f $basedir/flag";

exit;
